import folium

import math

def bd09_to_wgs84(lat, lon):
    # BD09 -> GCJ02
    x_pi = math.pi * 3000.0 / 180.0
    x = lon - 0.0065
    y = lat - 0.006
    z = math.sqrt(x * x + y * y) - 0.00002 * math.sin(y * x_pi)
    theta = math.atan2(y, x) - 0.000003 * math.cos(x * x_pi)
    gcj_lon = z * math.cos(theta)
    gcj_lat = z * math.sin(theta)
    
    # GCJ02 -> WGS84（近似逆向计算，误差约1-10米）
    a = 6378245.0
    ee = 0.00669342162296594323
    dlat = _transform_lat(gcj_lon - 105.0, gcj_lat - 35.0)
    dlon = _transform_lon(gcj_lon - 105.0, gcj_lat - 35.0)
    rad_lat = gcj_lat / 180.0 * math.pi
    magic = math.sin(rad_lat)
    magic = 1 - ee * magic * magic
    sqrt_magic = math.sqrt(magic)
    dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrt_magic) * math.pi)
    dlon = (dlon * 180.0) / (a / sqrt_magic * math.cos(rad_lat) * math.pi)
    wgs_lat = gcj_lat - dlat
    wgs_lon = gcj_lon - dlon
    return [wgs_lat, wgs_lon]

def _transform_lat(x, y):
    ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * math.sqrt(abs(x))
    ret += (20.0 * math.sin(6.0 * x * math.pi) + 20.0 * math.sin(2.0 * x * math.pi)) * 2.0 / 3.0
    ret += (20.0 * math.sin(y * math.pi) + 40.0 * math.sin(y / 3.0 * math.pi)) * 2.0 / 3.0
    ret += (160.0 * math.sin(y / 12.0 * math.pi) + 320 * math.sin(y * math.pi / 30.0)) * 2.0 / 3.0
    return ret

def _transform_lon(x, y):
    ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * math.sqrt(abs(x))
    ret += (20.0 * math.sin(6.0 * x * math.pi) + 20.0 * math.sin(2.0 * x * math.pi)) * 2.0 / 3.0
    ret += (20.0 * math.sin(x * math.pi) + 40.0 * math.sin(x / 3.0 * math.pi)) * 2.0 / 3.0
    ret += (150.0 * math.sin(x / 12.0 * math.pi) + 300.0 * math.sin(x / 30.0 * math.pi)) * 2.0 / 3.0
    return ret



def map(coordinates_ori):

    # 批量转换坐标
    coordinates = [bd09_to_wgs84(lat, lon) for lat, lon in coordinates_ori]
    print (coordinates)
    import folium
    from folium.plugins import AntPath  # 动画路径插件

    # # 示例坐标列表（需替换为实际数据）
    # coordinates = [
    #     [39.9042, 116.4074],  # 北京
    #     [31.2304, 121.4737],  # 上海
    #     [23.1291, 113.2644]   # 广州
    # ]

    # 1. 创建地图对象（自动适配坐标范围）
    m = folium.Map(location=coordinates[0], zoom_start=5)

    # 2. 绘制路径（支持多种样式）
    # 基础路径
    folium.PolyLine(
        locations=coordinates,
        color='#FF5733',        # 橙色
        weight=5,               # 线宽
        opacity=0.8,            # 透明度
        popup="路径轨迹"
    ).add_to(m)

    # 动画路径（需安装antpath插件）
    AntPath(
        locations=coordinates,
        color='#33FF57',
        weight=5,
        opacity=0.9
    ).add_to(m)

    # # 3. 添加路径节点标记
    # for idx, (lat, lon) in enumerate(coordinates):
    #     folium.Marker(
    #         location=[lat, lon],
    #         popup=f"节点 {idx+1}",
    #         icon=folium.Icon(
    #             icon='circle',      # 圆形标记
    #             color='#4B0082',    # 紫色
    #             fill=True,
    #             fill_opacity=0.7
    #         )
    #     ).add_to(m)

    # 4. 自动调整地图视野
    m.fit_bounds(m.get_bounds())

    # 5. 保存地图（支持离线查看）
    m.save("interactive_route.html")